package md.utm.university.dao;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import md.utm.university.data.MockPersons;
import md.utm.university.model.AbstractPerson;
import md.utm.university.model.Student;

public class PersonManager {
	private final MockPersons mockPersons;

	/**
	 * Instantiates a new person manager.
	 */
	public PersonManager() {
		mockPersons = new MockPersons();
		this.addPersons(new Student("person", "has", 1991));
	}

	/**
	 * Gets the persons.
	 * 
	 * @return the persons
	 */
	public List<AbstractPerson> getPersons() {
		List<AbstractPerson> persons = mockPersons.getPersons();
		Collections.sort(persons, new ComparablePerson());
		return persons;
	}

	public void addPersons(AbstractPerson person) {
		mockPersons.getPersons().add(person);
	}

	public void removePerson(final AbstractPerson person) {
		mockPersons.getPersons().remove(person);
	}

	public AbstractPerson findPerson(String name) {
		AbstractPerson person = null;
		List<AbstractPerson> persons = getPersons();
		for (AbstractPerson abstractPerson : persons) {
			if (abstractPerson.getName().equalsIgnoreCase(name)) {
				person = abstractPerson;
				break;
			}
		}
		return person;
	}

	private class ComparablePerson implements Comparator<AbstractPerson> {

		@Override
		public int compare(AbstractPerson o1, AbstractPerson o2) {
			return o1.getName().compareTo(o2.getName());
		}

	}
	
	public Boolean ageBetween(String name, int min, int max) {
		AbstractPerson person = null;
		int age;
		
		person = this.findPerson(name);
		age = 2013 - person.getYearOfBearth();
		
		if(age < min) {
			return false;
		} else if(age > max) {
			return false;
		}
		
		return true;
	}
}
